package com.topboya.linker.ui.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.topboya.linker.R;
import com.topboya.linker.model.levelitem.DepartmentGroupBo;
import com.topboya.linker.utils.LogUtils;

import java.util.List;
public class DepartMemberAdapter extends RecyclerView.Adapter<DepartMemberAdapter.ListItemViewHolder> {

    private static final String TAG = "DepartMemberAdapter:";
    private List<DepartmentGroupBo> departmetGroupList;

    public DepartMemberAdapter(List<DepartmentGroupBo> departmetGroupList) {

        this.departmetGroupList = departmetGroupList;

    }

    @Override
    public ListItemViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {

        View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.select_member_item, viewGroup, false);
        return new ListItemViewHolder(itemView);

    }


    //绑定界面，设置监听
    @Override
    public void onBindViewHolder(final ListItemViewHolder holder, final int i) {
        LogUtils.v(TAG+"onBindViewHolder");

        String  departname = departmetGroupList.get(i).getDeparemtname();
        String  departnamepic = departname.substring(departname.length()-2);
        holder.tv_member_name.setText(departname);
        holder.tv_head_pic.setText(departnamepic);
        LogUtils.v(TAG + "departname-------"+departname);

        holder.itemView.setOnClickListener(v -> clickListenter(holder,i));
        holder.tv_head_pic.setOnClickListener(v -> setItemChildClickListener(holder.tv_head_pic,i));

    }

    private void clickListenter(ListItemViewHolder holder, int position) {
        holder.itemView.setOnClickListener(v -> {

            if (null !=itemClickListener){
                itemClickListener.onClick(holder.itemView,position);
            }
        });
    }

    private void setItemChildClickListener(View view ,int position){

         itemChildClickListener.onclick(view,position);
    }

    @Override
    public int getItemCount() {

        return departmetGroupList == null ? 0 : departmetGroupList.size();
    }

    public class ListItemViewHolder extends RecyclerView.ViewHolder{

        //ViewHolder
        TextView tv_head_pic;
        TextView tv_member_name;

        ListItemViewHolder(View view) {

            super(view);
            this.tv_head_pic = view.findViewById(R.id.tv_head_pic);
            this.tv_member_name = view.findViewById(R.id.tv_member_name);

        }

    }


    private OnItemClickListener itemClickListener;

    public void setOnItemClickListener(DepartMemberAdapter.OnItemClickListener itemClickListener){

        this.itemClickListener = itemClickListener;
    }

    public interface  OnItemClickListener{

        void onClick(View v, int position);
    }


    private OnItemChildClickListener itemChildClickListener;

    public void setOnitemChildCkickListener(OnItemChildClickListener itemChildClickListener){

        this.itemChildClickListener = itemChildClickListener;
    }
    public interface  OnItemChildClickListener{

        void onclick(View view,int position);
    }

}
